
package com.bee.common.web.aspect;

import java.time.Clock;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.bee.common.core.enums.ClientEnum;
import com.bee.common.core.exception.BusinessException;
import com.bee.common.core.result.Result;
import com.bee.common.core.result.ResultCode;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.WebUtils;

import com.bee.common.core.log.persistence.entity.LogParameter;
import com.bee.common.core.log.persistence.entity.LogRequest;
import com.bee.common.core.log.service.ILogParameterService;
import com.bee.common.core.log.service.ILogRequestService;
import com.bee.common.tools.web.IpUtil;

import net.sf.json.JSONObject;


@Component
@Aspect
public class ControllerLogAspect {
	
	private static	Logger log = LoggerFactory.getLogger(ControllerLogAspect.class);
	
	@Autowired
	private ILogRequestService logServiceImpl;
	@Autowired
	private ILogParameterService logParameterServiceImpl;

	 @Before(value = "execution(public * com.bee..*"
	 		+ ".controller..*.*(..))" )
     public void beforeExecute(JoinPoint joinPoint) {
		 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
					.getRequestAttributes()).getRequest();
		    request.setAttribute("startTime", Clock.systemDefaultZone().millis());

		    LogRequest logRequest=new LogRequest();
		    
		    if(!StringUtils.isEmpty(request.getAttribute("userId"))){
		    	logRequest.setCommonUserId(request.getAttribute("userId").toString());
		    }
		    logRequest.setUrl(request.getServletPath());
			logRequest.setRequestIp(IpUtil.getIpAddr(request));
			logRequest.setCreatTime(String.valueOf(new Date().getTime()));
			logServiceImpl.addRequestUrlAsyn(logRequest);
			Map<String,Object> params=WebUtils.getParametersStartingWith(request, null);
			if(!params.isEmpty()){
				LogParameter logParameter=new LogParameter();
				logParameter.setRequestId(logRequest.getId());
				logParameter.setParameter(JSONObject.fromObject(params).toString());
				logParameterServiceImpl.addRequestParameterAsyn(logParameter);
			}
		//获取方法名  
	    String methodName = joinPoint.getSignature().getName(); 
	    String className = joinPoint.getTarget().getClass().getSimpleName();
	    log.info("request url is {}\n",request.getRequestURL().toString());
	    log.info(" Start {} {}\n",className, methodName);
	   
    }
    
     @After(value = "execution(public * com.bee..*.controller..*.*(..))" )
     public void afterExecute(JoinPoint joinPoint) {
    	 //获取类名
    	 String className=joinPoint.getTarget().getClass().getSimpleName();
    	 log.info("End {} {}\n", className,joinPoint.getSignature().getName());
    	 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
					.getRequestAttributes()).getRequest();
    	log.info("{} take time {} \n ",className +"."+ joinPoint.getSignature().getName(), Clock.systemDefaultZone().millis()- (long)request.getAttribute("startTime"));
    }
     

     
 	
}